'use strict';

exports.main = async (event, context) => {
	const { orderId, userId } = event;
	
	if (!userId) {
		return {
			code: -1,
			message: '用户未登录'
		}
	}
	
	if (!orderId) {
		return {
			code: -1,
			message: '订单ID不能为空'
		}
	}
	
	const db = uniCloud.database();
	const orderCollection = db.collection('orders');
	
	try {
		// 检查订单是否存在且属于当前用户
		const orderRes = await orderCollection
			.where({
				_id: orderId,
				userId: userId
			})
			.get();
			
		if (orderRes.data.length === 0) {
			return {
				code: -1,
				message: '订单不存在'
			}
		}
		
		// 更新订单状态为已取消
		// await orderCollection.doc(orderId).update({
		// 	status: 'cancelled',
		// 	cancelTime: Date.now()
		// });
		
		await orderCollection.doc(orderId).remove()
		return {
			code: 0,
			message: '取消成功'
		}
	} catch (error) {
		return {
			code: -1,
			message: '取消失败'
		}
	}
}; 